import $ from 'jquery'
import XLSX from "xlsx"
var data = {
    cols: [{ name: "A", key:0 },{ name: "8", key:1 },{ name: "C", key: 2}],
    data: [
        ["id","name","value"],[1,"sheetjs",7262],[2,"js-xlsx", 6969]
    ]
}
var aoa = [
    ["id","name","value"],[1,"sheetjs",7262],[2,"js-xlsx", 6969]
]
export default function bar(){
  console.info("hello")
  var d = document.createElement("button")
  d.innerHTML="点击生成excel文件"
  $(d).click(function(){
      exportxls()
  })
  $(d).attr("style","margin: 20px")
  var read = $("<input type='file' ></input>")
  read.change(function(){
    readxls(this.files[0])
  })
  $("body").append(d)
  $("body").append("<br/>")
  $("body").append("<label>读取文件</label>")
  $("body").append(read)
}

export function sayhello(){
    console.info("hello world")
}

export function readxls(file,callback){
    console.info(typeof file)
    var reader = new FileReader()
    reader.onload = function(e){
        var data = e.target.result
        var workbook = XLSX.read(data,{type: 'binary'})
        if(callback)callback(workbook)
        outputWorkbook(workbook)
    }
    reader.readAsBinaryString(file)
}
function outputWorkbook(workbook) {
    var sheetNames = workbook.SheetNames; // 工作表名称集合
    $("div.json").remove()
	sheetNames.forEach(name => {
        var worksheet = workbook.Sheets[name]; // 只能通过工作表名称来获取指定工作表
        var json = XLSX.utils.sheet_to_json(worksheet)
        $("body").append("<div class='json'>"+JSON.stringify(json)+"</div>")
		// for(var key in worksheet) {
		// 	// v是读取单元格的原始值
		// 	console.log(key, key[0] === '!' ? worksheet[key] : worksheet[key].v);
		// }
	});
}
export function exportxls(){   
    var sheet = XLSX.utils.aoa_to_sheet(aoa)
    openDownloadDialog(sheet2blob(sheet),"hello.xlsx")
}
export function sheet2blob(sheet, sheetName) {
	sheetName = sheetName || 'sheet1';
	var workbook = {
		SheetNames: [sheetName],
		Sheets: {}
	};
	workbook.Sheets[sheetName] = sheet;
	// 生成excel的配置项
	var wopts = {
		bookType: 'xlsx', // 要生成的文件类型
		bookSST: false, // 是否生成Shared String Table，官方解释是，如果开启生成速度会下降，但在低版本IOS设备上有更好的兼容性
		type: 'binary'
	};
	var wbout = XLSX.write(workbook, wopts);
	var blob = new Blob([s2ab(wbout)], {type:"application/octet-stream"});
	// 字符串转ArrayBuffer
	function s2ab(s) {
		var buf = new ArrayBuffer(s.length);
		var view = new Uint8Array(buf);
		for (var i=0; i!=s.length; ++i) view[i] = s.charCodeAt(i) & 0xFF;
		return buf;
	}
	return blob;
}
export function openDownloadDialog(url, saveName)
{
	if(typeof url == 'object' && url instanceof Blob)
	{
		url = URL.createObjectURL(url); // 创建blob地址
	}
	var aLink = document.createElement('a');
	aLink.href = url;
	aLink.download = saveName || ''; // HTML5新增的属性，指定保存文件名，可以不要后缀，注意，file:///模式下不会生效
	var event;
	if(window.MouseEvent) event = new MouseEvent('click');
	else
	{
		event = document.createEvent('MouseEvents');
		event.initMouseEvent('click', true, false, window, 0, 0, 0, 0, 0, false, false, false, false, 0, null);
	}
	aLink.dispatchEvent(event);
}